﻿using Ionic.Zip;
//using ICSharpCode.SharpZipLib.Zip;
using System;
using System.Collections.Generic;
using System.IO;
//using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Docx2Epub.Epub
{
    public class EpubPackager
    {
        private string inputdir;
        private string outputfile;

        public EpubPackager(string inputdir, string outputfile)
        {
            this.inputdir = Path.GetFullPath(inputdir);
            this.outputfile = outputfile;
        }


        public void WriteOut()
        {
            using (ZipFile zip = new ZipFile())
            {
                zip.CompressionLevel = 0;
                zip.EmitTimesInWindowsFormatWhenSaving = false;
                zip.AddEntry("mimetype", Encoding.ASCII.GetBytes("application/epub+zip"));

                zip.CompressionLevel = Ionic.Zlib.CompressionLevel.Default;

                //zip.AddDirectory("META-INF");
                var files = new DirectoryInfo(inputdir).GetFiles("*.*", SearchOption.AllDirectories);
                foreach (var f in files.Where(f => f.Name != "mimetype"))
                    zip.AddFile(f.FullName, Path.GetDirectoryName(f.FullName).Replace(inputdir + @"\", "").Replace(@"\", "/"));

                zip.Save(outputfile);
            }
        }

    }
}
